package devops.katas.reviewservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class ReviewserviceApplication {

	public static void main(String[] args) {
		SpringApplication.run(ReviewserviceApplication.class, args);
	}

}

@RestController
class ReviewServiceController {
	@GetMapping("/ratings")
	String getRating() {
		return "5";
	}
}

class User {
}

interface UserRetriever {
    User user(int id);
}

class UserService {

	private final UserRetriever userRetriever;

	public UserService(UserRetriever userRetriever) {
		this.userRetriever = userRetriever;
	}

	public List<User> users() {
		List<User> users = new ArrayList<>();
		users.add(this.userRetriever.user(1));
		users.add(this.userRetriever.user(2));
		return users;
	}
}

class HttpUserRetriever implements UserRetriever {

	private RestOperations restTemplate;

	public HttpUserRetriever(RestOperations restTemplate) {
		this.restTemplate = restTemplate;
	}

	@Override
	public User user(int id) {
		return this.restTemplate.getForObject("http://localhost:8081/user/{id}", User.class, id);
	}
}

@Configuration
class UserConfiguration {
	@Bean
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	@Bean
	UserRetriever userRetriever() {
		return new HttpUserRetriever(restTemplate());
	}

	@Bean
	UserService userService(UserRetriever userRetriever) {
		return new UserService(userRetriever);
	}
}